#coding=utf-8

require "aio/core"

class Aio::Module::Cmd::H3C::DisplayMemory < Aio::Module::Cmd::H3C
  include Aio::Module

  def initialize
    super({
      :cmd_full 		=> "display memory",
      :cmd_short		=> "dis mem",
      :author				=> "Elin",
      :description  => "This is H3C Command# display memory",
      :ranking			=> Ranking_1,
      :platform			=> "all",
      :benchmark		=> {}
    })
  end

  def parse
    cont = self.context.dup
    #memory = {}
    #useful[:memory] = memory

    case device_template
    when Template_3
      parse_template_3(cont)
    else
      parse_default(cont)
    end
  end

  def parse_default(cont)
    useful[:memory] = {}
    cont.readline_match_block(/System Total Memory\(bytes\): (?<total_memory>\d+)/) do |block|
      #block.update(memory, :total_memory)
    end
    cont.readline_match_block(/Total Used Memory\(bytes\): (?<used_memory>\d+)/) do |block|
      #block.update(memory, :used_memory)
    end
    cont.readline_match_block(/Used Rate: (?<used_rate>\d+)%/) do |block|
      block.update(useful[:memory], :used_rate)
    end
  end

  # MSR56-60 模板
  def parse_template_3(cont)
    useful[:memory] = {}
    cont.readline_match_block(/Total Used Free Shared Buffers Cached FreeRatio/) {|b|b}
    cont.readline_match_block(/Mem: .* (?<used_free>\d+\.\d)%$/) do |block|
      used = 100 - block[:used_free].to_f
      block.update(useful[:memory], :used_rate, used.to_s[0,4])
    end
  end
end

